pacman::p_load(dplyr, ggplot2, tidygeocoder, leaflet, sf, zipcodeR, tigris, tmap, RColorBrewer, here, readxl, RPostgres, DBI, keyring, spdep, gridExtra)Geowhatsup
Geospatial
There are several key libraries/object types for handling geospatial data in R.
sf: This is a library for handling simple feature objects. Simple features are a standard data protocol for geometry. Libraries may assume you already have an sf object, so if you’re reading data in from a csv, you may need to use sf to do conversion, further processing (spatial joins, etc). Read more heretigris: This is an interface to the census bureau’s TIGER shapefiles. Manually keeping and loading shapefiles in a pain in the butt. These download as sf objects, which is nice so you don’t have to do too much conversion.
The rest is kind of a hodgepodge of packages for helping you deal with the data. There are really so many different packages meant for mapping data, that it really depends on your familiarity with what you want to use for actual plotting.
If tmap is being annoying, you may need to pacman::p_load(XML) first… It’s weird!
Some online books that are helpful:
add note sp outdated
Up and running with base maps
If you are running analysis at some arbitrary geospatial level, you will typically aggregate that to that level and then join it to a sf object for mapping. tigris is super easy to use and a good way to get those base maps.
texas.counties <-
tigris::counties(cb=FALSE, state = "TX") Retrieving data for the year 2022
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================ | 24%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|===================================== | 54%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================ | 64%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================= | 94%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
names(texas.counties) [1] "STATEFP" "COUNTYFP" "COUNTYNS" "GEOID" "NAME" "NAMELSAD"
[7] "LSAD" "CLASSFP" "MTFCC" "CSAFP" "CBSAFP" "METDIVFP"
[13] "FUNCSTAT" "ALAND" "AWATER" "INTPTLAT" "INTPTLON" "geometry"
Notice the geometry column! GEOID is the FIPS code, which is just the other two FIPS codes put together.
class(texas.counties)[1] "sf" "data.frame"
We have a sf object. Which you can think of as a data.frame with additional attributes like a coordinate reference system (CRS). That means how the world is projected from 3 dimensions to 2. It matters and it doesn’t… Often times you will get weird errors about CRS… But as we can see, this is a NAD83 CRS:
st_crs(texas.counties)Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]
ggplot
ggplot may be the best bet if you are already familiar with the syntax. geom_sf is the key change that is different than what you might use.
ggplot(texas.counties) +
geom_sf(aes(fill=AWATER))
tmaps
tmaps is also good for plotting. You’ll see it makes some assumptions about breaks in continuous variables
tm_shape(texas.counties) +
tm_polygons("AWATER") 
leaflet
If you want to make interactive maps (if you end up making Shiny dashboards, you might use leaflet) leaflet is a good choice.
leaflet map code can get pretty complex, and might require a lot more finagling. We will get an error related to the CRS! But we will ignore it for now.
pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
leaflet(texas.counties) %>%
addPolygons(
stroke = FALSE,
fillColor = ~pal_fun(AWATER),
fillOpacity = 0.8,
popup = texas.counties$NAMELSAD) %>%
addTiles() Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'